//
// Created by 倪加奇 on 17-1-23.
// Email: drnijq@126.com
// Copyright (c) 2017 杭州威固网络科技有限公司. All rights reserved.
//

#ifndef TCP_ECHO_SERVER_TCP_ECHO_CONNETION_H
#define TCP_ECHO_SERVER_TCP_ECHO_CONNETION_H

#include <iostream>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/system/error_code.hpp>
#include <boost/bind.hpp>

using boost::asio::ip::tcp;

class tcp_echo_connetion : public boost::enable_shared_from_this<tcp_echo_connetion> {
public:
    typedef boost::shared_ptr<tcp_echo_connetion> pointer;
    static pointer create_pointer (boost::asio::io_service &io_service);
    tcp::socket &socket ();
    void start ();
    void first_start ();

private:
    tcp_echo_connetion (boost::asio::io_service &io_service);
    tcp::socket _socket;
    boost::scoped_array<char> _arr_buf;
    std::string _str_host;
    unsigned short _ns_port;

    void handle_read (std::size_t data_len, const boost::system::error_code &error);
    void handle_write (const boost::system::error_code &error);
    void print_log (const std::string &info);
};


#endif //TCP_ECHO_SERVER_TCP_ECHO_CONNETION_H
